﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Routing;
using System.Web.UI;
using phudieuhungthinh.BL;
using phudieuhungthinh.BL.Model;
using phudieuhungthinh.Helpers;

namespace phudieuhungthinh.Pages
{
    public partial class CategoryPage : Page, IRoutablePage
    {
        private const int PageSize = 11;
        protected RequestContext _context;

        public int CategoryID
        {
            get
            {
                Int32 result;
                return (Int32.TryParse(Request.QueryString["cateid"], out result)) ? result : 0;
            }
        }

        public int PageNum1
        {
            get
            {
                Int32 result;
                return (Int32.TryParse(Request.QueryString["pagenum"], out result)) ? result : 1;
            }
        }

        public string Url
        {
            get { return RouteValue("url").ToString(); }
        }

        public int PageNum
        {
            get
            {
                Int32 result;
                return (Int32.TryParse(RouteValue("page") + string.Empty, out result)) ? result : 1;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                pnlCategory.Controls.Add(new LiteralControl(LayoutProducts()));
                pSlide.Controls.Add(new LiteralControl(LayoutSlide()));
            }
        }

        protected string LayoutProducts()
        {
            try
            {
                int rowCount;
                var html = new StringBuilder();

                var imCate = new CategoryImpl();
                CategoryModel cate = imCate.GetCategoryModel(Url);
                if (!string.IsNullOrEmpty(cate.Description))
                {
                    html.AppendFormat("<div id='cate-desc'>{0}</div>", cate.Description);
                }

                var im = new ProductImpl();
                List<ProductModel> items = im.PagingProducts(Url, out rowCount, PageSize, PageNum);

                html.Append("<div id='product-list'>");
                html.Append("<div id='list'>");
                html.Append("<ul>");

                for (int i = 0; i < items.Count; i++)
                {
                    if (i == 0 || i == 1)
                    {
                        html.Append("<li class='item bigger'>");
                    }
                    else
                    {
                        html.Append("<li class='item'>");
                    }
                    html.AppendFormat("<a href='/san-pham/{0}' class='overlay'></a>", items[i].Url);
                    html.Append("<div class='img'>");
                    if (items[i].ImageUrl != null)
                    {
                        // /Images/Products/thumb/
                        html.AppendFormat("<img src='/hinh-anh/phu-dieu/{0}' alt='{1}' />", items[i].Url,
                            items[i].Name);
                    }
                    html.Append("</div>");
                    html.AppendFormat("<div class='title'><span>{0}</span><span> ({1})</span></div>", items[i].Name,
                        items[i].ProductNumber);
                    html.Append("</li>");
                }
                html.Append("</ul>");
                html.Append("</div>");
                html.Append("<div class='cate'>");
                if (PageNum > 1)
                {
                    html.AppendFormat("<a id='prev-cate' class='nav' href='/chuyen-muc/{0}/{1}'>Quay lại</a>", Url,
                        PageNum - 1);
                }
                if (PageNum * PageSize < rowCount)
                {
                    html.AppendFormat("<a id='next-cate' class='nav' href='/chuyen-muc/{0}/{1}'>Xem tiếp</a>", Url,
                        PageNum + 1);
                }
                html.Append("</div>");
                html.Append("</div>");

                return html.ToString();
            }
            catch
            {
                Response.Redirect(@"/loi-404");
                return string.Empty;
            }
        }

        protected string LayoutSlide()
        {
            try
            {
                int rowCount;
                var im = new ProductImpl();
                List<ProductModel> items = im.PagingProducts(Url, out rowCount, PageSize, PageNum + 1);

                if (items.Count > 0)
                {
                    var html =
                        new StringBuilder(
                            "<div class='slideshow' data-cycle-auto-height='3:1' data-cycle-fx='carousel' data-cycle-timeout='3000' data-cycle-timeout='0' data-cycle-carousel-visible='4' data-cycle-slides='> div' data-cycle-next='#next' data-cycle-prev='#prev' data-cycle-pager='#pager'>");

                    for (int i = 0; i < items.Count && i < 20; i++)
                    {
                        html.Append("<div class='item'>");
                        html.AppendFormat("<a href='/san-pham/{0}' class='overlay'></a>", items[i].Url);
                        html.Append("<div class='img'>");
                        if (items[i].ImageUrl != null)
                        {
                            html.AppendFormat(
                                "<img src='http://phudieudangcap.vn/images/products/thumb/{0}' alt='{1}' />",
                                items[i].ImageUrl, items[i].Name);
                        }
                        html.Append("</div>");
                        html.Append("</div>");
                    }

                    html.Append("</div>");
                    html.Append("<div class='center'>");
                    html.Append("<a href='#' id='prev' class='nav'></a><a href='#' id='next' class='nav'></a>");
                    html.Append("</div>");
                    html.Append("<div class='cycle-pager' id='pager'>");
                    html.Append("</div>");

                    return html.ToString();
                }
                return string.Empty;
            }
            catch
            {
                Response.Redirect(@"/loi-404");
                return string.Empty;
            }
        }

        protected object RouteValue(string key)
        {
            return _context.RouteData.Values[key];
        }

        #region IRoutablePage Members

        public RequestContext RequestContext
        {
            set { _context = value; }
        }

        #endregion
    }
}